/*
 * SniffEngine.h
 *
 *  Created on: Apr 3, 2013
 *      Author: root
 */

#ifndef SNIFFENGINE_H_
#define SNIFFENGINE_H_
#include "common.h"
#include <map>
using namespace std;

typedef struct
{
     uint8_t             			*packetData; // Raw packet data
    uint32_t             			packetLen;      // The actual length of the packet

    struct ether_header             *ethernetHeader;  // Ethernet header
    uint32_t						                    ethernetHeaderLen;

    struct ip                       *ipHeader;     // Internal IP header (Possibly encapsulated within another IP header such as with GRE)
    uint32_t						     ipHeaderLen;

    u_int8_t                        ipProtocol;


    struct tcphdr                   *tcpHeader;  // TCP header
    uint32_t	                                tcpHeaderLen;

    uint8_t						    *layer5Data;  // Layer 5 payload
    uint32_t	                    layer5DataLen;

    int32_t							remainingBufferLen; // Remaining bytes to be written on the target buffer. Should be SIGNED
    //FLV 							flvData;//FLV Header data.


} TPacketPointers;

/**********************************************************************************************
 * This struct set the direction of the packet as well if it is a sniffer only and
 * window size+ the devices names
 **********************************************************************************************/
typedef struct
{
	struct pcap      *hInternal;
	struct pcap      *hExternal;
	EPacketDirection curPacketDirection;

	char internalDevice[MAX_DEVICE_NAME];
	char externalDevice[MAX_DEVICE_NAME];

	u_int64_t 	internalPacketsIn;/*internal interface-how many packets enter from IN*/
	u_int64_t	internalPacketsOut;/*internal interface-how many packets got out*/
	u_int64_t	externalPacketsIn;/*external interface-how many packets enter from IN*/
	u_int64_t	externalPacketsOut;/*external interface-how many packets got out*/

	uint16_t  slidingWindowSize;//size of tcp window.
	int  	RTT;       // this value is in seconds need to be divided  in 10^6 for seconds!

	//HTTPsession *SessionTable[SESSION_TABLE_SIZE];
}SnifSTAT;

//void startDashSniffer(const char *internalDeviceStr, const char *externalDeviceStr);
void handelConnection(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);
STATE fillRequestPacketStruct(TPacketPointers *toFill, const uint8_t *bytes, u_int32_t len);
STATE forwardPacket(SnifSTAT *snifStatistics, const u_char *bytes, u_int32_t len, bool forceSend);
STATE respondPacket(SnifSTAT *snifStatistics, const u_char *bytes, u_int32_t len, bool forceSend);
STATE sendPacket(SnifSTAT *snifStatistics, const u_char *bytes, u_int32_t len, bool forceSend, bool isResponse);
void printStats(SnifSTAT *snifStatistics);
void printPacket(TPacketPointers *packet);



#endif /* SNIFFENGINE_H_ */
